// Copyright (c) 2024-present AI-Labs

// @ ts-nocheck
import { EditorEvents } from "@tiptap/core";
import { UAIEditorEventListener } from "@/interfaces/index.ts";
import { UAIEditorOptions } from "@/types/index.ts";
import { AppPlugin } from "@/plugins/AppPlugin.ts";

/**
 * 插件接口菜单按钮定义
 */
export abstract class AppPluginMenuButton extends HTMLElement implements UAIEditorEventListener {
    abstract onCreate(event: EditorEvents['create'], options: UAIEditorOptions): void;
    abstract onTransaction(event: EditorEvents['transaction'], options: UAIEditorOptions): void;
    abstract onEditableChange(editable: boolean): void;

    toggleContainer(plugin: AppPlugin, options: UAIEditorOptions) {
        // 遍历所有插件，关闭其他插件的窗口
        if (plugin.getContainer()) {
            // 判断窗口是否已打开
            const display = plugin.getContainer()!.style.display;
            if (display != "none") {
                // 如果窗口已打开，则关闭窗口
                plugin.getContainer()!.style.display = "none";
            } else {
                // 如果窗口没打开，则先关闭其他窗口，再打开窗口
                for (const slot in options.pluginRegistry?.getPlugins()) {
                    options.pluginRegistry?.getPlugins()[slot].forEach(plugin => {
                        if (plugin.getContainer()) {
                            plugin.getContainer()!.style.display = "none";
                        }
                    });
                }
                plugin.getContainer()!.style.display = "block";
            }
        }
    }
}